package com.geezdata.cps.sdk3rd.miya.request;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;

import com.geezdata.cps.base.utils.Md5Util;
import com.geezdata.cps.sdk3rd.miya.constant.Constants;
import com.geezdata.cps.sdk3rd.miya.response.MiyaSearchResult;

/**
 * 
 * @param vendorKey 店铺唯一标示 不可为空
 * @param startDate 查询开始时间 不可为空
 * @param endDate 查询结束时间 不可为空
 * @param orderState 订单状态 默认为 2 为空则为默认值, 其中  : 1.待付款；2.已付款待发货；3.发货中[预留，暂不使用]；4.发货完成；5.订单完结；6.已取消
 * @param page 查询的当前页数 不可为空
 * @param pageSize 页大小（min 1；max 100）不可为空
 * @param secretKey 店铺的秘钥  不可为空
 * @param dateType 默认为2,为空则为默认值 查询时间类型，默认按修改时间查询。1为按订单创建时间查询；其它数字为按订单修改时间
 */
public class MiyaSearchRequest extends MiyaRequest<MiyaSearchResult>{
	@NotBlank(message = "startDate 不能为空！")
    private String startDate;
	
	@NotBlank(message = "endDate 不能为空！")
    private String endDate;
	
	@NotBlank(message = "orderState 不能为空！")
    private String orderState = "2";
    
    @NotNull(message = "pageSize > 0!")
    @Min(1)
    private String page = "1";
    
    @NotNull(message = "pageSize 1-100!")
    @Min(1)
    @Max(100)
    private String pageSize = "20";
    
    @NotBlank(message = "dateType 不能为空！")
    private String dateType = "2";
    
    
    /*
     * 可选参数
     */
    private String format;
    private String version;
    private String optionalField;
    private String dateSort;
    
    {
        this.methodName =Constants.METHOD_MIYA_ORDER_SEARCH;
    }
    
    public String getStartDate() {
        return startDate;
    }
    
    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }
    
    public String getEndDate() {
        return endDate;
    }
    
    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }
    
    public String getOrderState() {
        return orderState;
    }
    
    public void setOrderState(String orderState) {
        this.orderState = orderState;
    }
    
    public String getPage() {
        return page;
    }
    
    public void setPage(String page) {
        this.page = page;
    }
    
    public String getPageSize() {
        return pageSize;
    }
    
    public void setPageSize(String pageSize) {
        this.pageSize = pageSize;
    }
    
    public String getFormat() {
        return format;
    }
    
    public void setFormat(String format) {
        this.format = format;
    }
    
    public String getVersion() {
        return version;
    }
    
    public void setVersion(String version) {
        this.version = version;
    }
    
    public String getOptionalField() {
        return optionalField;
    }
    
    public void setOptionalField(String optionalField) {
        this.optionalField = optionalField;
    }
    
    public String getDateSort() {
        return dateSort;
    }
    
    public void setDateSort(String dateSort) {
        this.dateSort = dateSort;
    }
    
    public String getDateType() {
        return dateType;
    }
    
    public void setDateType(String dateType) {
        this.dateType = dateType;
    }
    
    private void generateSign() {
        
        this.timestamp = Long.toString(System.currentTimeMillis()/1000);
        
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("timestamp", timestamp);
        hashMap.put("vendor_key", vendorKey);
        hashMap.put("method", methodName);
        hashMap.put("start_date", startDate);
        hashMap.put("end_date", endDate);
        hashMap.put("order_state", orderState);
        hashMap.put("page", page);
        hashMap.put("page_size", pageSize);
        hashMap.put("date_type", dateType);
        
        StringBuffer forsignBuffer = new StringBuffer();
        
        Object[] key_arr = hashMap.keySet().toArray();  
        Arrays.sort(key_arr);  
        for  (Object key : key_arr) {  
            Object value = hashMap.get(key); 
            forsignBuffer.append(key.toString()+value.toString());
        }          
        forsignBuffer.append(secretKey);
        String sign = Md5Util.md5(forsignBuffer.toString());  
        this.setSign(sign);
    }

    @Override
    public Map<String, Object> getAppParams() {
        this.generateSign();
        Map<String, Object> httpParams = new HashMap<String, Object>();  
        httpParams.put("secret_key", secretKey);
        httpParams.put("vendor_key", vendorKey);
        httpParams.put("method", methodName);
        httpParams.put("timestamp", timestamp);
        httpParams.put("sign", this.getSign());
        httpParams.put("order_state", orderState);
        httpParams.put("start_date", startDate);
        httpParams.put("end_date", endDate);
        httpParams.put("page", page);
        httpParams.put("page_size", pageSize);
        httpParams.put("date_type", dateType);
        return httpParams;
    }

    @Override
    public Class<MiyaSearchResult> getResponseClass() {     
        return MiyaSearchResult.class;
    }
}
